package com.dkm.ec.activity.domain;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.exception.PartinException;
import com.dkm.biz.gl.exception.StockDeficiencyException;
import com.dkm.biz.ou.repository.ShopStatRepository;
import com.dkm.ec.activity.persistence.dao.SecKillActivityPartinDao;
import com.dkm.ec.activity.persistence.dao.SecSkillActivitySkuDao;
import com.dkm.ec.activity.persistence.entity.SecKillActivityPartinPo;
import com.dkm.ec.activity.persistence.entity.SecSkillActivitySkuPo;
import com.dkm.ec.activity.repository.SecKillActivityPartinRepository;
import com.dkm.ec.stock.domain.Stock;
import com.dkm.ec.stock.persistence.dao.StockQueryDao;
import com.dkm.ec.stock.persistence.entity.StockPo;
import com.dkm.ec.stock.repository.StockRepository;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by fanjinyuan on 2017/8/11.
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class SecKillActivityPartin extends AbstractDomain<String, SecKillActivityPartinPo> {

    private SecKillActivityPartinDao secKillActivityPartinDao;

    @Resource
    private ShopStatRepository shopStatRepository;
    @Resource
    private StockQueryDao stockQueryDao;
    @Resource
    private StockRepository stockRepository;
    @Resource
    private SecSkillActivitySkuDao secSkillActivitySkuDao;
    @Resource
    private SecKillActivityPartinRepository secKillActivityPartinRepository;


    protected void init(){
        secKillActivityPartinDao = SpringHelper.getBean(SecKillActivityPartinDao.class);
        setDao(secKillActivityPartinDao);
    }


    public void partin(SecKillActivityPartinPo secKillActivityPartinPo) throws StockDeficiencyException {
        // 判断商家是否已参加该活动
        Integer count = secKillActivityPartinRepository.countForShopId(secKillActivityPartinPo.getActivityid(), secKillActivityPartinPo.getShopid());
        if(count >= 1) {
            throw new PartinException("同一个商家不能参加同一次活动");
        }
        // 合并相同sku
        List<SecSkillActivitySkuPo> skuPoList = mergeSku(secKillActivityPartinPo.getSkuPoList());
        // 判断库存是否不足
        for(SecSkillActivitySkuPo skuPo : skuPoList) {
            StockPo stockPo = stockQueryDao.get(skuPo.getStockid());
            int stock = stockPo.getQty() - stockPo.getQtyReserved();
            if(skuPo.getPartinnum()>stock)
                throw new StockDeficiencyException(String.format("sku: %s 库存不足", stockPo.getSkuId()));
            // 减少总库存数
            stockPo.setQty(stockPo.getQty() - skuPo.getPartinnum());
            Stock stockIns = stockRepository.newInstance(stockPo);
            stockIns.update();
        }

        //写入参与记录
        secKillActivityPartinPo.setPartintime(new Date());
        this.setData(secKillActivityPartinPo);
        this.create();
        // 写入sku
        for(SecSkillActivitySkuPo skuPo : skuPoList) {
            skuPo.setPartinid(secKillActivityPartinPo.getId());
            skuPo.setAvailableNum(skuPo.getPartinnum());
            skuPo.setIsReturnStock("0");
            secSkillActivitySkuDao.create(skuPo);
        }
    }

    public List<SecSkillActivitySkuPo> mergeSku(List<SecSkillActivitySkuPo> skuPoList) {
        Map<String, SecSkillActivitySkuPo> map = new HashMap<String, SecSkillActivitySkuPo>();
        for (SecSkillActivitySkuPo po : skuPoList) {
            String stockId = po.getStockid();
            if (map.containsKey(stockId)) {
                Integer partiNnum = map.get(stockId).getPartinnum();
                partiNnum += po.getPartinnum();
                map.get(stockId).setPartinnum(partiNnum);
            } else {
                map.put(stockId, po);
            }
        }
        List<SecSkillActivitySkuPo> list = new ArrayList<SecSkillActivitySkuPo>();
        Iterator<SecSkillActivitySkuPo> iterator = map.values().iterator();
        while(iterator.hasNext()) {
            SecSkillActivitySkuPo next = iterator.next();
            list.add(next);
        }
        return list;
    }



}
